package cn.tedu.erp.project.service.impl;


import cn.tedu.erp.project.ex.ServiceException;
import cn.tedu.erp.project.mapper.PurchaseSuppliersMapper;
import cn.tedu.erp.project.pojo.dto.PurchaseSuppliersAddNewDTO;
import cn.tedu.erp.project.pojo.dto.PurchaseSuppliersUpdateDTO;
import cn.tedu.erp.project.pojo.entity.PurchaseSuppliers;
import cn.tedu.erp.project.pojo.vo.PurchaseSuppliersListVO;
import cn.tedu.erp.project.pojo.vo.PurchaseSuppliersStandardVO;
import cn.tedu.erp.project.pojo.vo.PurchaseSuppliersListVO;
import cn.tedu.erp.project.pojo.vo.PurchaseSuppliersStandardVO;
import cn.tedu.erp.project.service.IPurchaseSuppliersService;
import cn.tedu.erp.project.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class PurchaseSuppliersServiceImpl implements IPurchaseSuppliersService {
    @Autowired
    PurchaseSuppliersMapper mapper;
    @Override
    public void addNew(PurchaseSuppliersAddNewDTO purchaseSuppliersAddNewDTO) {
        log.debug("开始处理【添加供应商】的业务，参数：{}", purchaseSuppliersAddNewDTO);
        // 调用参数对象的getName()得到尝试添加的供应商的名称
        String name = purchaseSuppliersAddNewDTO.getName();
        // 调用Mapper对象的countByName()执行统计查询
        int count = mapper.countByName(name);
        log.debug("根据名称【{}】统计数量，结果：{}", name, count);
        // 判断统计结果是否大于0
        if (count > 0) {
            // 是：名称被占用，抛出异常
            String message = "添加供应商失败，供应商名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        // 创建PurchaseSuppliers对象
        PurchaseSuppliers purchaseSuppliers = new PurchaseSuppliers();
        // 调用BeanUtils.copyProperties(源,目标)将参数对象中的属性复制到PurchaseSuppliers对象中
        BeanUtils.copyProperties(purchaseSuppliersAddNewDTO, purchaseSuppliers);
        // 调用Mapper对象的insert()执行插入供应商数据
        log.debug("即将执行插入数据，参数：{}", purchaseSuppliers);
        mapper.insert(purchaseSuppliers);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除供应商】的业务，参数：{}", id);
        // 调用Mapper对象的getStandardById()执行查询
        PurchaseSuppliersStandardVO purchaseSuppliers = mapper.getStandardById(id);
        log.debug("根据ID={}检查供应商数据是否存在，查询结果：{}", id, purchaseSuppliers);
        // 判断查询结果是否为null
        if (purchaseSuppliers == null) {
            // 是：数据不存在，抛出异常
            String message = "删除供应商失败，尝试删除的供应商数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 调用Mapper对象的deleteById()方法执行删除
        log.debug("即将执行删除，参数：{}", id);
        mapper.deleteById(id);
    }

    @Override
    public void updateInfoById(Long id, PurchaseSuppliersUpdateDTO purchaseSuppliersUpdateDTO) {
        log.debug("开始处理【修改供应商详情】的业务，ID：{}，新数据：{}", id, purchaseSuppliersUpdateDTO);
        // 调用Mapper对象的getStandardById()执行查询
        PurchaseSuppliersStandardVO queryResult = mapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：抛出异常
            String message = "修改供应商详情失败，尝试修改的供应商数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 调用Mapper对象的countByNameAndNotId()执行统计
        int count = mapper.countByNameAndNotId(id, purchaseSuppliersUpdateDTO.getName());
        // 判断统计结果是否大于0
        if (count > 0) {
            // 是：名称被占用，抛出异常
            String message = "修改供应商详情失败，供应商名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        // 创建PurchaseSuppliers对象
        PurchaseSuppliers purchaseSuppliers = new PurchaseSuppliers();
        // 复制属性，设置ID
        BeanUtils.copyProperties(purchaseSuppliersUpdateDTO, purchaseSuppliers);
        purchaseSuppliers.setId(id);
        // 调用Mapper对象的update()方法执行修改
        mapper.update(purchaseSuppliers);
    }

    @Override
    public PurchaseSuppliersStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询供应商详情】的业务，参数：{}", id);
        // PurchaseSuppliersStandardVO queryResult = mapper.getStandardById(id);
        PurchaseSuppliersStandardVO queryResult = mapper.getStandardById(id);
        if (queryResult == null) {
            // 是：抛出异常
            String message = "查询供应商详情失败，供应商数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public List<PurchaseSuppliersListVO> list() {
        log.debug("开始处理【查询供应商列表】的业务，无参数");
        // List<PurchaseSuppliersListItemVO> list = mapper.list();
        List<PurchaseSuppliersListVO> list = mapper.list();
        return list;
    }

    @Override
    public void rebuildCache() {

    }
}
